#include <unistd.h>  
#include <stdlib.h>  
#include <stdio.h>  
#include <sys/shm.h>  

typedef struct shared_memory{
	int flag;
	char text[2048];
};
int main()
{
	int flag = 1;
	struct shared_memory *addr = NULL;
	void *shmaddr   = NULL;
	int shmid;
	int running = 1;
	
	shmid = shmget((key_t)2345, sizeof(struct shared_memory), 0660|IPC_CREAT | IPC_EXCL);
	if(shmid == -1)
	{	
		shmid = shmget((key_t)2345, sizeof(struct shared_memory), 0660 | IPC_EXCL);
		if(shmid == -1) return -1;
	}

	shmaddr = shmat(shmid, 0, 0);
	if(shmaddr == (void *) -1)
	{
		shmctl(shmid, IPC_RMID, NULL);
		return -1;
	}
	addr = (struct shared_memory *)shmaddr;
	addr->flag = 0;
	while(running)
	{
		if(addr->flag != 0)
		{
			printf("your write is %s", addr->text);
			sleep(rand() % 3); 
			addr->written = 0; 
			if(strmcp(addr->text, "end") == 0)
			{
				running = 0;
			}
		}
		else
		{
			sleep(1);
		}
	}

	if(-1 == shmdt(shmid)) return -1;

	if(shmctl(shmid, IPC_RMID, NULL) == -1) return -1;

	return 0;
}
